!function () {
    /**
     * 配置 requirejs
     */
    requirejs.config(REQUIREJS_CONFIG);
    utils.initHandlebars();

    /**
     * 程序入口
     */
    $.when(publicApi('dicts'), publicApi('app_menu')).then(function (dicts, homeData) {
        if (homeData.message) {
            if (homeData.code === 401) {
                publicApi('logout');
                location.reload();
            } else {
                alert(homeData.message);
                location.href = '/';
            }
            return;
        }

        utils.initDict(dicts);

        $('#main-header').html(utils.getHtml('header', homeData));
        $('#main-sidebar').html(utils.getHtml('sidebar', homeData));
        $('#main-wrapper').removeClass('hidden');
        $('#loading-panel').remove();

        $.AdminLTE.layout.fixSidebar();
        $.AdminLTE.pushMenu.expandOnHover();
        $.AdminLTE.layout.activate();

        /**
         * 滚动菜单
         */
        var swiper = new Swiper('#swiper-menu', {
            slidesPerView: 'auto',
            nextButton: '.swiper-next',
            prevButton: '.swiper-prev'
        });

        /**
         * 初始化一个Tab
         */
        var initPane = function (view, tab, pane) {
            requirejs([view], function (init) {
                init(pane);
            });
        }

        /**
         * 初始化路由
         */
        var tabs = $('#navbar-tabs'), main = $('#main-content');
        Router({
            '/:view': function (view) {
                //左边菜单选中效果
                $("#main-sidebar .sidebar-menu li.active").removeClass('active');
                var selected = $('#main-sidebar a[href="#/' + view + '"]');
                selected.parents('li').addClass('active');

                //添加Tab
                var tabId = 'tab_' + view, tab = $('#' + tabId);
                if (!tab.length) {
                    tab = $('<li id="' + tabId + '" class="swiper-slide" style="width: auto;"><a href="#' + view + '" data-toggle="tab">' + selected.html() +
                        '</a><i data-value="' + view + '" class="close-tab glyphicon glyphicon-remove"></i></li>');
                    swiper.appendSlide(tab);

                    var pane = $('<div class="tab-pane" id="' + view + '"></div>').appendTo(main);
                    initPane(view, tab, pane);
                }

                //选中Tab
                $('a', tab).tab('show');
                swiper.slideTo(tab.index());
            }
        }).init();

        /**
         * 初始化tab事件
         */
        $('#navbar-tabs').on('click', 'a[data-toggle="tab"]', function () {
            location.hash = $(this).attr('href');
        }).on('shown.bs.tab', function (e) {
            var self = $(e.target);
            var pane = $(self.attr('href'));
            if (pane.length) {
                var tables = $('.table', pane);
                if (tables.length) {
                    tables.eq(0).DataTable().fixedHeader.enable(true);
                }
            }
        }).on('hide.bs.tab', function (e) {
            var self = $(e.target);
            var pane = $(self.attr('href'));
            if (pane.length) {
                var tables = $('.table', pane);
                if (tables.length) {
                    tables.eq(0).DataTable().fixedHeader.enable(false);
                }
            }
        }).on('click', '.close-tab', function () {
            //关闭Tab
            var tabs = $('#navbar-tabs a[data-toggle="tab"]'), li = $(this).parents('li');
            var len = tabs.length;
            if (len < 2) {
                return false;
            }
            var index = li.index();
            if (li.hasClass('active')) {
                var newTab;
                if (index + 1 < len) {
                    newTab = tabs.eq(index + 1);
                } else {
                    newTab = tabs.eq(index - 1);
                }
                newTab.tab('show');
                newTab[0].click();
            }
            var value = $(this).attr('data-value');
            swiper.removeSlide(index);

            var tables = $('#' + value + ' .table');
            if (tables.length) {
                tables.DataTable().destroy();
            }
            $('#' + value).remove();
        });

        /**
         * 默认选中第一个菜单
         */
        if (!location.hash) {
            location.hash = $('#main-sidebar .sidebar-menu a').eq(0).attr('href');
            if (!location.hash) {
                location.hash = $('#main-sidebar .sidebar-menu .treeview-menu a').eq(0).attr('href');
            }
        }
    }, function () {
        $('#loading-panel').remove();
    });

    /**
     * 退出登录
     */
    $(document).on('click', '.logout', function () {
        noty({
            layout: 'topLeft',
            type: 'confirm',
            theme: 'relax',
            text: '你确认退出登录?',
            buttons: [
                {
                    addClass: 'btn btn-primary', text: '取消',
                    onClick: function ($noty) {
                        $noty.close();
                    }
                },
                {
                    addClass: 'btn btn-danger', text: '确认',
                    onClick: function () {
                        location.href = '/logout';
                    }
                }
            ]
        });
    });

    /**
     * socket.io
     */
    // var playSound = function (url) {
    //     if (!url) url = '/admin/mp3/alert.mp3';
    //     $('#play-sound').html("<embed src='" + url + "' hidden='true' autostart='true' loop='false' class='playSound'>" + "<audio autoplay='autoplay' style='display:none;' controls='controls'><source src='" + url + "' /></audio>");
    // }
    //
    // var socket = io.connect();
    // socket.on('notification', function (data) {
    //     if (data.type == 'NewOrder') {
    //         playSound();
    //     }
    //     console.log(data);
    // });
}();